package com.ruoyi.web.wx.controller;

import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;

public class RedPackage {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入红包金额数：");
        double money = sc.nextDouble();
        System.out.println("请输入红包个数:");
        int num = sc.nextInt();

        BigDecimal[] arr = new RedPackage().red(new BigDecimal(money), num);

        double count=0;
        for (int i = 0; i < arr.length; i++) {
            System.out.println("第"+i+"个人的红包数是：" + arr[i].doubleValue());
            count+=arr[i].doubleValue();
        }
        System.out.println("合计总数"+count);
    }

    /**
     * @param money 发红包的金额
     * @param num   人数
     * @return
     * @throws RuntimeException
     */
    public BigDecimal[] red(BigDecimal money, int num) throws RuntimeException {
        if (money.doubleValue() < num * 0.01) {
            throw new RuntimeException("金额不对....");
        }

        Random random = new Random();
        //使用分 进行计算金额
        int allMoney = money.multiply(BigDecimal.valueOf(100)).intValue();

        int count = 0;   //存储所有人的随机点数和
        double[] perCount = new double[num];//存储每一个人的随机点数

        BigDecimal[] perMoney = new BigDecimal[num]; //存储每一个人的红包金额

        // 给每一个人分配一个随机数
        for (int i = 0; i < perCount.length; i++) {
            int r = random.nextInt((num) * 99) + 1;
            count += r;
            perCount[i] = r;
        }


        int c = 0;
        //分配红包金额
        for (int i = 0; i < perCount.length; i++) {
            Double x = new Double(perCount[i] / count);
            int m = (int) Math.floor(x * allMoney);
            if (m == 0) {
                m = 1;
            }
            c += m;

            if (i < perCount.length - 1) {
                perMoney[i] = (new BigDecimal(m).divide(new BigDecimal(100)));
            } else {
                perMoney[i] = (new BigDecimal(allMoney - c + m).divide(new BigDecimal(100)));
            }
        }

        return perMoney;
    }



}
